﻿package we3d.modeler.create
{
	import we3d.scene.SceneObject;
	import we3d.material.Surface;
	
	/**
	* Create a grid of lines in a SceneObject
	*/
	public class GridTool
	{
		/** 
		* Add a grid to a SceneObject
		* @param	obj	object for the lines and points
		* @param	sf	surface for polygons
		* @param	grid_size	size of the grid
		* @param	grid_lines	lines in the grid * 2
		* @param	yPos		the y position of the points
		*/
		public static function create (obj:SceneObject, sf:Surface, grid_size:Number=100, grid_lines:int=16, yPos:Number=0) :void {
			
			var g:Number = grid_size / grid_lines;
			var g2:Number = grid_size/2;
			
			var x:Number = -g2;
			var z:Number = -g2+g;
			var y:Number = yPos || 0;
			
			var i:int;
			var L:int = grid_lines-1;
			
			for(i=0; i<L; i++) {
				obj.addPoint(x, y, z);
				obj.addPoint(g2, y, z);
				obj.addPolygon( sf, obj.points.length-2, obj.points.length-1);
				z += g;
			}
			
			x = -g2+g;
			z = -g2;
			
			for(i=0; i<L; i++) {
				obj.addPoint(x, y, z);
				obj.addPoint(x, y, g2);
				obj.addPolygon(sf, obj.points.length-2, obj.points.length-1);
				x += g;
			}
		}
		
	}
}